package com.demo.springboot.server.controller;

import com.demo.springboot.server.bean.Product;
import com.demo.springboot.server.mappers.ProductMapper;
import com.demo.springboot.server.web.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProductController {

    @Autowired
    private ProductMapper productMapper;

    /***
     * 首页
     */
    @GetMapping("/")
    public String home() {
        return "<ul>"
            + "<li><a href='/soa/product/list'>产品清单</a></li>"
            + "</ul>";
    }

    @PostMapping("/soa/product/add")
    public Response add(Product product) {
        Integer result = productMapper.add(product);
        if (result == 1) {
            return new Response(200, "数据插入成功");
        }
        return new Response(500, "数据插入失败");
    }

    @PutMapping("/soa/product/update")
    public Response update(Product product) {
        Integer result = productMapper.update(product);
        if (result == 1) {
            return new Response(200, "数据更新成功", product);
        }
        return new Response(500, "数据更新失败");
    }

    @DeleteMapping("/soa/product/delete/{id}")
    public Response delete(@PathVariable("id") Integer id) {
        Integer result = productMapper.deleteById(id);
        if (result == 1) {
            return new Response(200, "数据删除成功", id);
        }
        return new Response(500, "数据删除失败");
    }

    @GetMapping("/soa/product/get/{id}")
    public Response get(@PathVariable("id") Integer id) {
        Product result = productMapper.getById(id);
        if (result != null) {
            return new Response(200, "数据查询成功", result);
        }
        return new Response(500, "数据查询失败");
    }

    @GetMapping("/soa/product/list")
    public Response list() {
        List<Product> results = productMapper.queryLists();
        if (results != null) {
            return new Response(200, "数据查询成功", results);
        }
        return new Response(500, "数据查询失败");
    }

    @GetMapping("/soa/product/version")
    public Response version() {
        return new Response(200, "成功","127.0.0.1:8080");
    }
}
